package applicationAlibi;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.*;

public class generationFacturePDF {
	
	private static Font basefont = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD); 
	private static Font smallfont = new Font(Font.FontFamily.TIMES_ROMAN, 11); 
	private String nomEntreprise, adresseEntreprise,villeEntreprise, 
	codePostalEntreprise,year,  month,  day, produit1, produit2,
	produit3,  produit4, produit5, TVA1, TVA2, TVA3 , TVA4, TVA5,
	nom, prenom, adresse, ville,qt1,qt2,qt3,qt4,qt5,p1,p2,p3,p4,p5;
	
	
	public generationFacturePDF(String nomEntreprise, String adresseEntreprise, String villeEntreprise, 
			String codePostalEntreprise,String year, String month, String day, String produit1, String produit2,
			String produit3, String produit4, String produit5, String TVA1, String TVA2, String TVA3, String TVA4, String TVA5,
			String nom, String prenom, String adresse, String ville, String qt2, String qt1, String qt3, String qt4, String qt5, String p1, String p2, String p3, String p4, String p5) throws IOException{
		this.nomEntreprise=nomEntreprise;
		this.adresseEntreprise=adresseEntreprise;
		this.villeEntreprise= villeEntreprise;
		this.codePostalEntreprise=codePostalEntreprise;
		this.year=year;
		this.month=month;
		this.day=day;
		this.produit1=produit1;
		this.produit2=produit2;
		this.produit3=produit3;
		this.produit4=produit4;
		this.produit5=produit5;
		this.TVA1=TVA1;
		this.TVA2=TVA2;
		this.TVA3=TVA3;
		this.TVA4=TVA4;
		this.TVA5=TVA5;
		this.nom=nom;
		this.prenom=prenom;
		this.adresse=adresse;
		this.ville=ville ;
		this.qt1=qt1;
		this.qt2=qt2;
		this.qt3=qt3;
		this.qt4=qt4;
		this.qt5=qt5;
		this.p1=p1;
		this.p2=p2;
		this.p3=p3;
		this.p4=p4;
		this.p5=p5;
		Document doc = new Document(); 
		String cheminFichier = "facture.pdf" ;
		try {
			PdfWriter.getInstance(doc, new FileOutputStream(cheminFichier));
			doc.open(); 
			
			// on remplit le pdf
			addAdresseEntreprise(doc,this.day,this.month,this.year,this.nomEntreprise,this.adresseEntreprise,this.villeEntreprise,this.codePostalEntreprise);
			addAdresseDestinataire(doc,this.nom,this.prenom, this.adresse,this.ville);
			addDescriptif(doc,this.produit1,this.produit2,this.produit3,this.produit4,this.produit5,this.TVA1,this.TVA2,this.TVA3,this.TVA4,this.TVA5,this.qt1,this.qt2,this.qt3,this.qt4,this.qt5,this.p1,this.p2,this.p3,this.p4,this.p5);
			
			doc.close();
			// on lance le pdf
			Runtime.getRuntime().exec("evince " + cheminFichier); 
		
		} catch (DocumentException e) {
			e.printStackTrace();
		} 
	}

	public static void addAdresseEntreprise(Document doc, String day, String month, String year, String nomEntreprise, String adresseEntreprise, String villeEntreprise, String codePostalEntreprise) throws DocumentException{
		
		Paragraph entreprise = new Paragraph();
		entreprise.add(new Paragraph(nomEntreprise,basefont));
		entreprise.add(new Paragraph(adresseEntreprise,basefont));
		entreprise.add(new Paragraph(codePostalEntreprise+" "+villeEntreprise, basefont));
		entreprise.add(new Paragraph("N�SIRET 732 829 320 00074",basefont));
		addEmptyLine(entreprise,3);
		Paragraph date= new Paragraph("Date de facturation : "+day+" / "+month+" / "+year,basefont);
		date.setIndentationLeft(100);
		entreprise.add(date);
		addEmptyLine(entreprise,2);
		Random r = new Random();
		int valeur = 1000 + r.nextInt(999999 - 1000);
		Paragraph ref = new Paragraph("Reference : Facture "+valeur,basefont);
		ref.setIndentationLeft(70);
		entreprise.add(ref);
		valeur = 10000 + r.nextInt(999999-10000);
		Paragraph numClient = new Paragraph("Num�ro de client : "+valeur,basefont);
		numClient.setIndentationLeft(70);
		entreprise.add(numClient);
		doc.add(entreprise);
	}

	public static void addAdresseDestinataire(Document doc, String nom, String prenom, String adresse, String ville) throws DocumentException{
		Paragraph destinataire = new Paragraph();
		destinataire.add(new Paragraph(nom+" "+ prenom,basefont));
		destinataire.add(new Paragraph(adresse,basefont));
		destinataire.add(new Paragraph(ville,basefont));
		destinataire.setAlignment(Element.ALIGN_RIGHT);
		addEmptyLine(destinataire,4);
		doc.add(destinataire);
	}
	
	public static void addDescriptif(Document doc, String produit1,String produit2, String produit3, String produit4, String produit5, String TVA1, String TVA2, String TVA3, String TVA4, String TVA5, String qt1, String qt2, String qt3, String qt4, String qt5, String p1, String p2, String p3, String p4, String p5) throws DocumentException{
		Paragraph descriptif = new Paragraph();
		PdfPTable table = new PdfPTable(6);
		
		float montantHT55 = 0;
		float montantHT196 = 0;
		float montantTTC55 = 0;
		float montantTTC196 = 0;
		
		PdfPCell c1 = new PdfPCell(new Phrase("R�f�rence",basefont));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		c1 = new PdfPCell(new Phrase("D�signation",basefont));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		c1 = new PdfPCell(new Phrase("Quantit�",basefont));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		c1 = new PdfPCell(new Phrase("Prix H.T",basefont));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		c1 = new PdfPCell(new Phrase("Taux TVA",basefont));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		c1 = new PdfPCell(new Phrase("TOTAL H.T",basefont));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		
		int quantite1 = Integer.parseInt(qt1);
		float prix1 = Float.parseFloat(p1);
		String ht1 =String.valueOf(prix1 * quantite1);
		Random r = new Random();
		int valeur = 100000 + r.nextInt(999999 - 100000);
		String ref1 = String.valueOf(valeur);
		
		if (TVA1.equals("19,6")){
			montantHT196 += prix1*quantite1;
		}
		else{
			montantHT55 +=prix1*quantite1;
		}
		
		table.addCell(new Phrase(ref1,smallfont));
		table.addCell(new Phrase(produit1,smallfont));
		table.addCell(new Phrase(qt1,smallfont));
		table.addCell(new Phrase(p1,smallfont));
		table.addCell(new Phrase(TVA1,smallfont));
		table.addCell(new Phrase(ht1,smallfont));
		
		if (!produit2.equals("")){
			int quantite2 = Integer.parseInt(qt2);
			float prix2 = Float.parseFloat(p2);
			String ht2 =String.valueOf(prix2 * quantite2);
			valeur = 100000 + r.nextInt(999999 - 100000);
			String ref2 = String.valueOf(valeur);
			table.addCell(new Phrase(ref2,smallfont));
			table.addCell(new Phrase(produit2,smallfont));
			table.addCell(new Phrase(qt2,smallfont));
			table.addCell(new Phrase(p2,smallfont));
			table.addCell(new Phrase(TVA2,smallfont));
			table.addCell(new Phrase(ht2,smallfont));
			if (TVA2.equals("19,6")){
				montantHT196 += prix2*quantite2;
			}
			else{
				montantHT55 +=prix2*quantite2;
			}
		}
		
		if (!produit3.equals("")){
			int quantite3 = Integer.parseInt(qt3); 
			float prix3 = Float.parseFloat(p3);
			String ht3 =String.valueOf(prix3 * quantite3);
			valeur = 100000 + r.nextInt(999999 - 100000);
			String ref3 = String.valueOf(valeur);
			table.addCell(new Phrase(ref3,smallfont));
			table.addCell(new Phrase(produit3,smallfont));
			table.addCell(new Phrase(qt3,smallfont));
			table.addCell(new Phrase(p3,smallfont));
			table.addCell(new Phrase(TVA3,smallfont));
			table.addCell(new Phrase(ht3,smallfont));
			if (TVA3.equals("19,6")){
				montantHT196 += prix3*quantite3;
			}
			else{
				montantHT55 +=prix3*quantite3;
			}
		}

		if (!produit4.equals("")){
			int quantite4 = Integer.parseInt(qt4);
			float prix4 = Float.parseFloat(p4);
			String ht4 =String.valueOf(prix4 * quantite4);
			valeur = 100000 + r.nextInt(999999 - 100000);
			String ref4 = String.valueOf(valeur);
			table.addCell(new Phrase(ref4,smallfont));
			table.addCell(new Phrase(produit4,smallfont));
			table.addCell(new Phrase(qt4,smallfont));
			table.addCell(new Phrase(p4,smallfont));
			table.addCell(new Phrase(TVA4,smallfont));
			table.addCell(new Phrase(ht4,smallfont));
			if (TVA4.equals("19,6")){
				montantHT196 += prix4*quantite4;
			}
			else{
				montantHT55 +=prix4*quantite4;
			}
		}

		if (!produit5.equals("")){
			int quantite5 = Integer.parseInt(qt5);
			float prix5 = Float.parseFloat(p5);
			String ht5 =String.valueOf(prix5 * quantite5);
			valeur = 100000 + r.nextInt(999999 - 100000);
			String ref5 = String.valueOf(valeur);
			table.addCell(new Phrase(ref5,smallfont));
			table.addCell(new Phrase(produit5,smallfont));
			table.addCell(new Phrase(qt5,smallfont));
			table.addCell(new Phrase(p5,smallfont));
			table.addCell(new Phrase(TVA5,smallfont));
			table.addCell(new Phrase(ht5,smallfont));
			if (TVA5.equals("19,6")){
				montantHT196 += prix5*quantite5;
			}
			else{
				montantHT55 +=prix5*quantite5;
			}
		}		
		descriptif.add(table);
		
		addEmptyLine(descriptif, 1);
		
		PdfPTable tableRecapitulativeHT = new PdfPTable(3);
		tableRecapitulativeHT.setWidthPercentage(50);
		PdfPCell c2 = new PdfPCell(new Phrase("Taux TVA",basefont));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableRecapitulativeHT.addCell(c2);
		c2 = new PdfPCell(new Phrase("5,5 %",basefont));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableRecapitulativeHT.addCell(c2);
		c2 = new PdfPCell(new Phrase("19,6 %",basefont));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableRecapitulativeHT.addCell(c2);
		c2 = new PdfPCell(new Phrase("Montant HT",basefont));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableRecapitulativeHT.addCell(c2);
		c2 = new PdfPCell(new Phrase(String.valueOf(montantHT55),smallfont));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableRecapitulativeHT.addCell(c2);
		c2 = new PdfPCell(new Phrase(String.valueOf(montantHT196),smallfont));
		c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableRecapitulativeHT.addCell(c2);
		
		descriptif.add(tableRecapitulativeHT);
		
		addEmptyLine(descriptif, 1);
		
		montantTTC55 = (float) (montantHT55 + montantHT55*5.5/100);
		montantTTC196 = (float) (montantHT196 + montantHT196*19.6/100);
		
		PdfPTable tableRecapitulativeTTC = new PdfPTable(3);
		tableRecapitulativeTTC.setWidthPercentage(50);
		PdfPCell c3 = new PdfPCell(new Phrase("Taux TVA",basefont));
		c3.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableRecapitulativeTTC.addCell(c3);
		c3 = new PdfPCell(new Phrase("5,5 %",basefont));
		c3.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableRecapitulativeTTC.addCell(c3);
		c3 = new PdfPCell(new Phrase("19,6 %",basefont));
		c3.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableRecapitulativeTTC.addCell(c3);
		c3 = new PdfPCell(new Phrase("Montant TTC",basefont));
		c3.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableRecapitulativeTTC.addCell(c3);
		c3 = new PdfPCell(new Phrase(String.valueOf(montantTTC55),smallfont));
		c3.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableRecapitulativeTTC.addCell(c3);
		c3 = new PdfPCell(new Phrase(String.valueOf(montantTTC196),smallfont));
		c3.setHorizontalAlignment(Element.ALIGN_CENTER);
		tableRecapitulativeTTC.addCell(c3);
		
		descriptif.add(tableRecapitulativeTTC);
		addEmptyLine(descriptif,1);
		doc.add(descriptif);
		
		float montantTTC2=montantTTC196+montantTTC55;
		float montantTTC3=montantTTC2*100;	
		float montantTTC4=Math.round(montantTTC3);
		float montantTTC=montantTTC4/100;
		
		Paragraph aPayer = new Paragraph("Net � payer TTC:\n"+String.valueOf(montantTTC)+"�",basefont);
		aPayer.setAlignment(Element.ALIGN_RIGHT);
		doc.add(aPayer);

	}

	
	private static void addEmptyLine(Paragraph paragraph, int number) {
		for (int i = 0; i < number; i++) {
			paragraph.add(new Paragraph(" "));
		}
	}
}
